<?php
class WxUser extends BaseActiveRecord
{
    /**
    *微信用户信息
    *wangw  wangwei@wanthings.com
    */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return '{{wx_user}}';
    }

    /**
    *微信用户列表
    */
     public function listByAttr($attr = array())
    {
        $result=array();
        $result['data']=array();
        $result['pages']=array();
        if (!empty($id)) {
            $criteria = new CDbCriteria();
            $criteria->order = 'id asc';
            $criteria->addColumnCondition(array('is_deleted'=>0));
            if (!empty($attr)) {
                if (!empty($attr['page']) && !empty($attr['page_size'])) {
                    $count = Article::model()->count($criteria);
                    $pages = new CPagination;
                    $pages->setItemCount($count);
                    $pages->setCurrentPage($page-1);
                    $pages->setPageSize($page_size);
                    $pages->validateCurrentPage = true;
                    $pages->applyLimit($criteria);
                    $return['pages'] = $pages;
                }
            }
            $data = $this->findAll($criteria);
            if (!empty($data)) {
                foreach ($data as $key => $value) {
                    $temp=array();
                    $temp=$value->attributes;
                    $result[$key]=$temp;
                }
            }
        }
        return $result;
    }
    public function bindPhone($openid='',$mobile='')
    {
        $result = 0;
        if (empty($mobile) || empty($openid)) {
            return $result;
        }
        $old = $this->detailByMobile($mobile);
        if (!empty($old)) {
            return -1;
        }
        $criteria = New CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted'=>0));
        if (!empty($openid)) {
            $criteria->addColumnCondition(array('openid'=>$openid));
        }
        $data = $this->find($criteria);
        if (!empty($data)) {
            $data->mobile = $mobile;
            if ($data->save()) {
                $result = $data->id;
            }
        }
        return $result;
    }
    public function bindUser($openid,$user_id)
    {
        $result = 0;
        if (empty($user_id) || empty($openid)) {
            return $result;
        }
        //清空原来绑定的userid
        $this->unBindUser($user_id);
        $criteria = New CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted'=>0));
        if (!empty($openid)) {
            $criteria->addColumnCondition(array('openid'=>$openid));
        }
        $data = $this->find($criteria);
        if (!empty($data)) {
            $data->mobile = $user_id;
            if ($data->save()) {
                //
                $wx_data['user_id'] = $user_id;
                $wx_data['openid'] = $data->openid;
                $wx_data['nickname'] = $data->nickname;
                $wx_data['sex'] = $data->sex;
                $wx_data['headimgurl'] = $data->headimgurl;
                User::model()->bindWechat($wx_data);
                $result = $data->id;
            }
        }
        return $result;
    }

    /**
    *新增一个用户
    */
    public function add($user =array())
    {
        $result = 0;
        //检查是否存在

        if (!empty($user) && !empty($user['openid'])) {

            $is_exist = $this->detailByOpenId($user['openid']);
            if ($is_exist) {
                $result = $this->edit($user);
                return $result;
                exit;
            }
            $wx_user = New WxUser;
            $wx_user->openid = $user['openid'];
            if (!empty($user['access_token'])) {
                $wx_user->access_token = $user['access_token'];
            }
            if (!empty($user['nickname'])) {
                $wx_user->nickname = $user['nickname'];
            }
            if (!empty($user['sex'])) {
                $wx_user->sex = $user['sex'];
            }
            if (!empty($user['language'])) {
                $wx_user->language = $user['language'];
            }
            if (!empty($user['city'])) {
                $wx_user->city = $user['city'];
            }
            if (!empty($user['province'])) {
                $wx_user->province = $user['province'];
            }
            if (!empty($user['country'])) {
                $wx_user->country = $user['country'];
            }
            if (!empty($user['headimgurl'])) {
                $wx_user->headimgurl = $user['headimgurl'];
            }
            if (!empty($user['subscribe_time'])) {
                $wx_user->subscribe_time = $user['subscribe_time'];
            }
            if (!empty($user['subscribe'])) {
                $wx_user->subscribe = $user['subscribe'];
            }
            if (!empty($user['unionid'])) {
                $wx_user->unionid = $user['unionid'];
            }
            if (!empty($user['remark'])) {
                $wx_user->remark = $user['remar'];
            }
            if (!empty($user['groupid'])) {
                $wx_user->groupid = $user['groupid'];
            }
            if (!empty($user['mobile'])) {
                $wx_user->mobile= $user['mobile'];
            }
            if (!empty($user['state'])) {
                $wx_user->state= $user['state'];
            }
            if (!empty($user['user_type'])) {
                $wx_user->user_type= $user['user_type'];
            }
            $wx_user->ctime=time();
            $wx_user->mtime=time();
            if ($wx_user->save()) {
                return $wx_user->id;
            }
        }
        return $result;
    }
    /**
    *编辑一个用户
    */
    public function edit($user =array())
    {
        $result = 0;
        //检查是否存在

        if (!empty($user) && !empty($user['openid']) ) {

            $criteria = new CDbCriteria();
            $criteria->order = 'id asc';
            $criteria->addColumnCondition(array('openid'=>$user['openid'],'is_deleted'=>0));
            $wx_user = $this->find($criteria);
            if (empty($wx_user)) {
                return $result;
            }
            $wx_user->openid = $user['openid'];
            if (!empty($user['access_token'])) {
                $wx_user->access_token = $user['access_token'];
            }
            if (!empty($user['nickname'])) {
                $wx_user->nickname = $user['nickname'];
            }
            if (!empty($user['sex'])) {
                $wx_user->sex = $user['sex'];
            }
            if (!empty($user['language'])) {
                $wx_user->language = $user['language'];
            }
            if (!empty($user['city'])) {
                $wx_user->city = $user['city'];
            }
            if (!empty($user['province'])) {
                $wx_user->province = $user['province'];
            }
            if (!empty($user['country'])) {
                $wx_user->country = $user['country'];
            }
            if (!empty($user['headimgurl'])) {
                $wx_user->headimgurl = $user['headimgurl'];
            }
            if (!empty($user['subscribe_time'])) {
                $wx_user->subscribe_time = $user['subscribe_time'];
            }
            if (!empty($user['subscribe'])) {
                $wx_user->subscribe = $user['subscribe'];
            } else {
                $wx_user->subscribe =0;
            }
            if (!empty($user['unionid'])) {
                $wx_user->unionid = $user['unionid'];
            }
            if (!empty($user['remark'])) {
                $wx_user->remark = $user['remar'];
            }
            if (!empty($user['groupid'])) {
                $wx_user->groupid = $user['groupid'];
            }
            if (!empty($user['mobile'])) {
                $wx_user->mobile= $user['mobile'];
            }
            if (!empty($user['state'])) {
                $wx_user->state= $user['state'];
            }
            if (!empty($user['user_type'])) {
                $wx_user->user_type= $user['user_type'];
            }
            $wx_user->mtime=time();
            if ($wx_user->save()) {
                return $wx_user->id;
            }
        }
        return $result;
    }
    /**
    *获取详情 通过手机号码
    */
    public function detailByMobile($mobile)
    {
        $result = array();
        if (!empty($mobile)) {
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('mobile'=>$mobile,'is_deleted'=>0));
            $wx_user = $this->find($criteria);
            if (!empty($wx_user)) {
                $result = $wx_user->attributes;
            }
        }
        return $result;
    }

    /**
    *获取详情 通过ID
    */
    public function detailById($id)
    {
        $result = array();
        if (!empty($id)) {
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('id'=>$id,'is_deleted'=>0));
            $wx_user = $this->find($criteria);
            if (!empty($wx_user)) {
                $result = $wx_user->attributes;
            }
        }
        return $result;
    }

    /**
    *获取详情 通过OPENID
    */
    public function detailByOpenId($id)
    {
        $result = array();
        if (!empty($id)) {
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('openid'=>$id,'is_deleted'=>0));
            $wx_user = $this->find($criteria);
            if (!empty($wx_user)) {
                $result = $wx_user->attributes;
            }
        }
        return $result;
    }

    /**
    *获取用户ID 通过OPENID
    */
    public function getUserIdByOpenId($id)
    {
        $result = '';
        $data = $this->detailByOpenId($id);
        if (!empty($data)) {
            if (!empty($data)) {
                $user = User::model()->detailByMobile($data['mobile']);
                if (!empty($user)) {
                    return $user['sid'];
                }
            }
        }
        return $result;
    }

    /**
    *获取用户ID 通过OPENID
    */
    public function getUserByOpenId($id)
    {
        $result = array();
        $data = $this->detailByOpenId($id);
        if (!empty($data)) {
            if (!empty($data) && !empty($data['mobile']) ) {
                $user = User::model()->detailByMobile($data['mobile']);
                if (!empty($user)) {
                    return $user;
                }
            }
        }
        return $result;
    }

    /**
    *获取详情 通过access_token
    */
    public function detailByWxToken($access_token)
    {
        $result = array();
        if (!empty($access_token)) {
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('access_token'=>$access_token,'is_deleted'=>0));
            $wx_user = $this->find($criteria);
            if (!empty($wx_user)) {
                $result = $wx_user->attributes;
            }
        }
        return $result;
    }

    /**
     * luhp 2016/06/29
     * 根据openid更改is_read的值
     */
    public function editIsreadbyopenid($openid)
    {
        $return = '';
        if(!empty($openid)){
            $criteria = new CDbCriteria;
            $criteria->addColumnCondition(array('is_deleted'=>0,'openid'=>$openid));
            $find = WxUser::model()->find($criteria);
            if(!empty($find)){
                $find->is_read = 1;
                if($find->save()){
                    $return = $openid;
                }
            }
        }
        return $return;
    }

    /**
     * luhp 2016/06/29
     * 根据openid获取用户is_read 值
     */
    public function getIsreadbyopenid($openid)
    {
        $return = '';
        if(!empty($openid)){
            $user = WxUser::model()->detailByOpenId($openid);
            if(!empty($user) && isset($user['is_read'])){
                $return = $user['is_read'];
            }
        }
        return $return;
    }
    /**
     * wangw 2016/06/29
     * 根据openid获取用户是否关注
     */
    public function getIsSubscribebyopenid($openid)
    {
        $return = false;
        if(!empty($openid)){
            $user = WxUser::model()->detailByOpenId($openid);
            if(!empty($user) && !empty($user['subscribe'])){
                $return = true;
            }
        }
        return $return;
    }

    //退出登录 解绑 方法
    public function unBindUser($user_id)
    {
        $result = false;
        $criteria = New CDbCriteria;
        $criteria->addColumnCondition(array('is_deleted'=>0,'mobile'=>$user_id));
        $data = $this->find($criteria);
        if (!empty($data)) {
            $data->mobile = '';
            $result = $data->save();
        }
        return $result;
    }

}
